package com.aixi.desktop.glance

import android.content.Context
import android.content.Intent
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.sp
import androidx.glance.GlanceId
import androidx.glance.GlanceModifier
import androidx.glance.action.clickable
import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.GlanceAppWidgetReceiver
import androidx.glance.appwidget.provideContent
import androidx.glance.appwidget.updateAll
import androidx.glance.text.FontWeight
import androidx.glance.text.Text
import androidx.glance.text.TextAlign
import androidx.glance.text.TextStyle
import androidx.glance.unit.ColorProvider
import com.aixi.desktop.data.AppConfigStore
import com.aixi.desktop.data.AppConfigStore.appConfigStore
import com.aixi.desktop.utils.sendMyWidgetAppUpdate
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import java.util.Calendar

class MyWidgetApp : GlanceAppWidgetReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        super.onReceive(context, intent)

        if (intent.action == ACTION) {
            val second = Calendar.getInstance().get(Calendar.SECOND)
            context.sendMyWidgetAppUpdate(System.currentTimeMillis() + 1000 * (60 - second))
        }
        upWidget(context)
    }

    override val glanceAppWidget: GlanceAppWidget
        get() = MyGlanceApp()

    class MyGlanceApp : GlanceAppWidget() {
        override suspend fun provideGlance(context: Context, id: GlanceId) {
            val calendar = Calendar.getInstance()

            val hour = calendar.get(Calendar.HOUR_OF_DAY);
            val minute = calendar.get(Calendar.MINUTE);

            val fontSize =
                context.appConfigStore.data.first()[AppConfigStore.STORE_KEY_WIDGET_INT_TIME_FONT_SIZE]
                    ?: 20
            val fontColorR =
                context.appConfigStore.data.first()[AppConfigStore.STORE_KEY_WIDGET_INT_TIME_FONT_COLOR_R]
                    ?: 0
            val fontColorG =
                context.appConfigStore.data.first()[AppConfigStore.STORE_KEY_WIDGET_INT_TIME_FONT_COLOR_G]
                    ?: 0
            val fontColorB =
                context.appConfigStore.data.first()[AppConfigStore.STORE_KEY_WIDGET_INT_TIME_FONT_COLOR_B]
                    ?: 0
            val fontColorA =
                context.appConfigStore.data.first()[AppConfigStore.STORE_KEY_WIDGET_INT_TIME_FONT_COLOR_A]
                    ?: 0xFF
            provideContent {
                Text(
                    text = "%02d %02d".format(hour, minute),
                    style = TextStyle(
                        color = ColorProvider(
                            Color(
                                fontColorR,
                                fontColorG,
                                fontColorB,
                                fontColorA
                            )
                        ),
                        fontSize = fontSize.sp,
                    ),
                    modifier = GlanceModifier.clickable {
                        context.sendMyWidgetAppUpdate()
                    },
                )
            }
        }
    }

    companion object {
        const val ACTION: String = "com.aixi.desktop.glance.MyWidgetApp.AUTO_UPDATE"

        fun upWidget(context: Context) {
            MainScope().launch {
                MyGlanceApp().updateAll(context)
            }
        }
    }
}